package com.iwomy.envpro.platform.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.envpro.common.core.util.R;
import com.iwomy.envpro.common.log.annotation.SysLog;
import com.iwomy.envpro.platform.dto.AlarmLoggingDTO;
import com.iwomy.envpro.platform.entity.AlarmLoggingEntity;
import com.iwomy.envpro.platform.service.AlarmLoggingService;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.envpro.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 报警记录
 *
 * @author pig
 * @date 2023-10-05 14:28:40
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/alarmLogging")
@Tag(description = "alarmLogging", name = "报警记录管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class AlarmLoggingController {

	private final AlarmLoggingService alarmLoggingService;

	/**
	 * 分页查询
	 *
	 * @param page            分页对象
	 * @param alarmLoggingDTO 报警记录
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('platform_alarmLogging_view')")
	public R getAlarmLoggingPage(@ParameterObject Page page, @ParameterObject AlarmLoggingDTO alarmLoggingDTO) {
		return R.ok(alarmLoggingService.getAlarmLoggingByPage(page, alarmLoggingDTO));
	}


	/**
	 * 通过id查询报警记录
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('platform_alarmLogging_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(alarmLoggingService.getDetails(id));
	}

	/**
	 * 新增报警记录
	 *
	 * @param alarmLogging 报警记录
	 * @return R
	 */
	@Operation(summary = "新增报警记录", description = "新增报警记录")
	@SysLog("新增报警记录")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('platform_alarmLogging_add')")
	public R save(@RequestBody AlarmLoggingEntity alarmLogging) {
		return R.ok(alarmLoggingService.save(alarmLogging));
	}

	/**
	 * 修改报警记录
	 *
	 * @param alarmLogging 报警记录
	 * @return R
	 */
	@Operation(summary = "修改报警记录", description = "修改报警记录")
	@SysLog("修改报警记录")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('platform_alarmLogging_edit')")
	public R updateById(@RequestBody AlarmLoggingEntity alarmLogging) {
		return R.ok(alarmLoggingService.updateAlarmLogging(alarmLogging));
	}

	/**
	 * 通过id删除报警记录
	 *
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除报警记录", description = "通过id删除报警记录")
	@SysLog("通过id删除报警记录")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('platform_alarmLogging_del')")
	public R removeById(@RequestBody Long[] ids) {
		return R.ok(alarmLoggingService.removeBatchByIds(CollUtil.toList(ids)));
	}


	/**
	 * 导出excel 表格
	 *
	 * @param alarmLogging 查询条件
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('platform_alarmLogging_export')")
	public List<AlarmLoggingEntity> export(AlarmLoggingEntity alarmLogging) {
		return alarmLoggingService.list(Wrappers.query(alarmLogging));
	}

	/**
	 * 根据设备ID查找最新的报警记录
	 *
	 * @param equipmentId
	 * @return
	 */
	@GetMapping("/getOneByEquipmentId/{equipmentId}")
	public R getOneByEquipmentId(@PathVariable("equipmentId") Long equipmentId) {

		LambdaQueryWrapper<AlarmLoggingEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(AlarmLoggingEntity::getEquipmentId, equipmentId);
		wrapper.eq(AlarmLoggingEntity::getEventStatus, 10);
		wrapper.orderByDesc(AlarmLoggingEntity::getCreateTime);
		wrapper.last("limit 1");
		return R.ok(alarmLoggingService.getOne(wrapper));
	}


/**************************APP接口****************************/


	/**
	 * 分页查询
	 *
	 * @param page            分页对象
	 * @param alarmLoggingDTO 报警记录
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/appPage")
	public R appPage(@ParameterObject Page page, @ParameterObject AlarmLoggingDTO alarmLoggingDTO) {
		return R.ok(alarmLoggingService.getAlarmLoggingByPage(page, alarmLoggingDTO));
	}


	/**
	 * 修改报警记录
	 *
	 * @param alarmLogging 报警记录
	 * @return R
	 */
	@Operation(summary = "修改报警记录", description = "修改报警记录")
	@SysLog("修改报警记录")
	@PutMapping("appUpdateById")
	public R appUpdateById(@RequestBody AlarmLoggingEntity alarmLogging) {
		return R.ok(alarmLoggingService.updateById(alarmLogging));
	}

	/**
	 * 通过id查询报警记录详情
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询报警记录详情", description = "通过id查询报警记录详情")
	@GetMapping("appGetById/{id}")
	public R appGetById(@PathVariable("id") Long id) {
		return R.ok(alarmLoggingService.getDetails(id));
	}


}